package chain

import (
	"fmt"
	"time"
)

//责任链模式
type Manager interface {
	HaveRight(money int) bool
	HandleFeeRequest(name string, money int) bool
}

//泽恩连肯定是有自己的
type RequestChain struct {
	Manager
	successor *RequestChain
}

func (r *RequestChain) SetSuccessor(m *RequestChain) {
	r.successor = m
}
func (r *RequestChain) HandleFeeRequest(name string, money int) bool {
	if r.Manager.HaveRight(money) {
		return r.Manager.HandleFeeRequest(name, money)
	}
	if r.successor != nil {
		return r.successor.HandleFeeRequest(name, money)
	}
	return false
}
func (r *RequestChain) HaveRight(money int) bool {
	return true
}

type ProjectManager struct{}

func NewProjectManagerChain() *RequestChain {
	return &RequestChain{
		Manager: &ProjectManager{},
	}
}
func (p *ProjectManager) HaveRight(money int) bool {
	return money < 500
}
func (p *ProjectManager) HandleFeeRequest(name string, money int) bool {
	if name == "bob" {
		fmt.Printf("Project manager permit %s %d fee request\n", name, money)
		return true
	}
	fmt.Printf("Project manager don't permit %s %d fee request\n", name, money)
	return false
}

type DepManager struct {
}

func NewDepManagerChain() *RequestChain {
	return &RequestChain{
		Manager: &DepManager{},
	}
}
func (*DepManager) HaveRight(money int) bool {
	return money < 5000
}
func (*DepManager) HandleFeeRequest(name string, money int) bool {
	if name == "tom" {
		fmt.Printf("Dep manager permit %s %d fee request\n", name, money)
		return true
	}
	fmt.Printf("Dep manager don't permit %s %d fee request\n", name, money)
	return false
}

type GeneralManager struct{}

func NewGeneralManagerChain() *RequestChain {
	return &RequestChain{
		Manager: &GeneralManager{},
	}
}

func (*GeneralManager) HaveRight(money int) bool {
	return true
}

func (*GeneralManager) HandleFeeRequest(name string, money int) bool {
	if name == "ada" {
		fmt.Printf("General manager permit %s %d fee request\n", name, money)
		return true
	}
	fmt.Printf("General manager don't permit %s %d fee request\n", name, money)
	return false
}

type Message interface {
	Notify() //通知
}

//我们进行使用责任链和装饰器和闭包和本身
type OrderDetail interface {
	IsEixtis(order, token string) bool
	GetOrderinfo(order string) map[string]interface{}
	HaveRight(money int) bool
}
type Order struct {
	OrderDetail
	Message
	successor *Order
}
type DefaultNotifyFactory interface {
	CreateMessageNotify() Message
}

var defautFactory DefaultNotifyFactory

func Init() error {
	if defautFactory != nil {
		defautFactory = &DefaultNoityfactoryImpl{}
	}
	return nil
}

type DefaultNoityfactoryImpl struct{}

func (d *DefaultNoityfactoryImpl) CreateMessageNotify() Message {
	return &DefaultNotfity{}
}

type DefaultNotfity struct {
}

func (d *DefaultNotfity) Notify() {

}

func (r *Order) SetSuccessor(m *Order) {
	r.successor = m
}
func (r *Order) IsEixtis(order, token string) bool {
	//代理封装和before after
	//本身也是 接口的实例
	//本身就剧透信息
	//如果不是 都可以传递 抱在肚子里 则 closer reader
	// 即可
	//只是没有实例化处理即可
	//这里可以进行箭筒和chul
	go func() {
		time.Sleep(time.Second)
		r.Message.Notify() //通知
		//异步监控和sucess 队列es-result 获取
		//将结果写入到日志中进行sucess
		//rpc.message.nmotofy
	}()
	return r.OrderDetail.IsEixtis(order, token)
}
